package com.blb.community_admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.community_admin.Annotation.Log;
import com.blb.community_admin.entity.SysRole;
import com.blb.community_admin.entity.SysUser;
import com.blb.community_admin.service.IRoleService;
import com.blb.community_admin.util.EasyExcelUtil;
import com.blb.community_admin.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@Api
public class RoleController {

    @Resource
    private IRoleService roleService;

    @ApiOperation("根据id搜索")
    @GetMapping("/roles/user-id/{id}")
    public ResponseResult<List<SysRole>> getRolesByUserId(@PathVariable Long id){
        return ResponseResult.ok(roleService.getRolesByUserId(id));
    }

    @ApiOperation("查询全部")
    @GetMapping("/roles")
    public ResponseResult<List<SysRole>> getAllRoles(){
        System.out.println("111111111111111");
        List<SysRole> list = roleService.list(null);
        System.out.println(list);
        return ResponseResult.ok(list);
    }

    @ApiOperation("模糊查询")
    @PostMapping("/roles/text")
    public ResponseResult<IPage<SysRole>> getByText(@RequestBody SysRole sysRole,Long current) {
        System.out.println(sysRole);
        IPage<SysRole> sysRole1 = roleService.getByText(new Page<>(current, 3L),
                sysRole);
        return ResponseResult.ok(sysRole1);
    }

    @Log(module = "角色管理", operationType = 1, requestMethod = "POST")
    @Transactional
    @ApiOperation("新增角色")
    @PostMapping("/roles/addroles")
    public ResponseResult<String> addRoles(@RequestBody  SysRole sysRole){
        LocalDateTime now = LocalDateTime.now();
        sysRole.setCreateTime(now);
        SysRole sysrole1 = roleService.getOne(new QueryWrapper<SysRole>().lambda().eq(SysRole::getRoleName, sysRole.getRoleName()));
        if (sysrole1 == null){

            roleService.save(sysRole);
            return ResponseResult.ok(""+sysRole.getRoleId());
        }
        return ResponseResult.ok("添加失败");

     }

    @Log(module = "角色管理", operationType = 3, requestMethod = "DELETE")
    @ApiOperation("删除")
    @DeleteMapping("/roles/del/{roleId}")
    public ResponseResult<String> delSysRole(@PathVariable Long roleId){
        System.out.println("删除方法，roid为："+roleId);
        roleService.deleteRoles(roleId);
        return ResponseResult.ok("删除成功");
    }

    @Log(module = "角色管理", operationType = 2, requestMethod = "PUT")
    @ApiOperation("修改状态")
    @PutMapping("/roles/status")
    public ResponseResult<String> updateStadus(@RequestBody SysRole sysRole){

        LocalDateTime now = LocalDateTime.now();
        sysRole.setUpdateTime(now);

        roleService.updateById(sysRole);
        return ResponseResult.ok("修改状态成功");
    }

    @Log(module = "角色管理", operationType = 2, requestMethod = "PUT")
    @ApiOperation("修改角色")
    @PostMapping("/roles/updateroles")
    public ResponseResult<SysRole> updateRoles(@RequestBody SysRole sysRole){
        LocalDateTime now = LocalDateTime.now();
        sysRole.setUpdateTime(now);
        roleService.updateById(sysRole);
        System.out.println(sysRole);
        return ResponseResult.ok(sysRole);
    }

    @Log(module = "角色管理", operationType = 5, requestMethod = "POST")
    @ApiOperation("导出")
    @PostMapping("/roles/export")
    public ResponseEntity<ByteArrayResource> export(@RequestBody List<SysRole> sysRole) throws IOException {
        String path = EasyExcelUtil.write("角色管理", SysRole.class, sysRole);
        ResponseEntity<ByteArrayResource> download = EasyExcelUtil.download(path);
        return download;
    }


}
